#include <wx/wx.h>
#include <wx/fontdlg.h>

class CustomDialog : public wxDialog
{
public:
    CustomDialog(const wxString &title);
};

CustomDialog::CustomDialog(const wxString &title)
    : wxDialog(NULL, -1, title, wxDefaultPosition, wxSize(250, 230))
{

    wxPanel *panel = new wxPanel(this, -1);

    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);

    wxStaticBox *st = new wxStaticBox(panel, -1, wxT("Colors"),
                                      wxPoint(5, 5), wxSize(240, 150));
    wxRadioButton *rb = new wxRadioButton(panel, -1,
                                          wxT("256 Colors"), wxPoint(15, 30), wxDefaultSize, wxRB_GROUP);

    wxRadioButton *rb1 = new wxRadioButton(panel, -1,
                                           wxT("16 Colors"), wxPoint(15, 55));
    wxRadioButton *rb2 = new wxRadioButton(panel, -1,
                                           wxT("2 Colors"), wxPoint(15, 80));
    wxRadioButton *rb3 = new wxRadioButton(panel, -1,
                                           wxT("Custom"), wxPoint(15, 105));
    wxTextCtrl *tc = new wxTextCtrl(panel, -1, wxT(""),
                                    wxPoint(95, 105));

    wxButton *okButton = new wxButton(this, -1, wxT("Ok"),
                                      wxDefaultPosition, wxSize(70, 30));
    okButton->Bind(wxEVT_BUTTON, [&](wxCommandEvent &event) { Destroy();});

    wxButton *closeButton = new wxButton(this, -1, wxT("Close"),
                                         wxDefaultPosition, wxSize(70, 30));
    closeButton->Bind(wxEVT_BUTTON, [&](wxCommandEvent &event) { Destroy();});

    hbox->Add(okButton, 1);
    hbox->Add(closeButton, 1, wxLEFT, 5);

    vbox->Add(panel, 1);
    vbox->Add(hbox, 0, wxALIGN_CENTER | wxTOP | wxBOTTOM, 10);

    SetSizer(vbox);

    Centre();
    ShowModal();

    Destroy();
}

class Messages : public wxFrame
{
public:
    Messages(const wxString &title);

    void ShowMessage1(wxCommandEvent &event);
    void ShowMessage2(wxCommandEvent &event);
    void ShowMessage3(wxCommandEvent &event);
    void ShowMessage4(wxCommandEvent &event);
    void ShowFileDialog(wxCommandEvent &event);
    void ShowFontDialog(wxCommandEvent &event);
    void ShowCustomDialog(wxCommandEvent &event);

    wxStaticText *st;
};

const int ID_INFO = 1;
const int ID_ERROR = 2;
const int ID_QUESTION = 3;
const int ID_ALERT = 4;
const int ID_OPEN = 5;

Messages::Messages(const wxString &title)
    : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
{
    wxInitAllImageHandlers();
    wxPanel *panel = new wxPanel(this, wxID_ANY);

    wxBoxSizer *hbox = new wxBoxSizer(wxVERTICAL);
    wxGridSizer *gs = new wxGridSizer(3, 3, 2, 2);

    st = new wxStaticText(panel, wxID_ANY, wxT("字体测试：Messages"));

    wxButton *btn1 = new wxButton(panel, ID_INFO, wxT("Info"));
    wxButton *btn2 = new wxButton(panel, ID_ERROR, wxT("Error"));
    wxButton *btn3 = new wxButton(panel, ID_QUESTION, wxT("Question"));
    wxButton *btn4 = new wxButton(panel, ID_ALERT, wxT("Alert"));
    wxButton *btn5 = new wxButton(panel, ID_OPEN, wxT("Open"));
    wxButton *btn6 = new wxButton(panel, wxID_ANY, wxT("Font Dialog"));
    wxButton *btn7 = new wxButton(panel, wxID_ANY, wxT("Custom Dialog"));

    Connect(ID_INFO, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(Messages::ShowMessage1));
    Connect(ID_ERROR, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(Messages::ShowMessage2));
    Connect(ID_QUESTION, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(Messages::ShowMessage3));
    Connect(ID_ALERT, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(Messages::ShowMessage4));
    Connect(ID_OPEN, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(Messages::ShowFileDialog));

    btn6->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &Messages::ShowFontDialog, this);
    btn7->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &Messages::ShowCustomDialog, this);

    gs->Add(btn1, 1, wxEXPAND);
    gs->Add(btn2, 1, wxEXPAND);
    gs->Add(btn3, 1, wxEXPAND);
    gs->Add(btn4, 1, wxEXPAND);
    gs->Add(btn5, 1, wxEXPAND);
    gs->Add(btn6, 1, wxEXPAND);
    gs->Add(btn7, 1, wxEXPAND);

    hbox->Add(gs, 1, wxALL, 5);
    hbox->Add(st, 1, wxALL, 5);
    panel->SetSizer(hbox);

    Center();
}

void Messages::ShowMessage1(wxCommandEvent &event)
{
    wxMessageDialog *dial = new wxMessageDialog(NULL,
                                                wxT("Download completed"), wxT("Info"), wxOK);
    dial->ShowModal();
}

void Messages::ShowMessage2(wxCommandEvent &event)
{
    wxMessageDialog *dial = new wxMessageDialog(NULL,
                                                wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR);
    dial->ShowModal();
}

void Messages::ShowMessage3(wxCommandEvent &event)
{
    wxMessageDialog *dial = new wxMessageDialog(NULL,
                                                wxT("Are you sure to quit?"), wxT("Question"),
                                                wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
    if (dial->ShowModal() == wxID_YES)
    {
        Destroy();
    }
}

void Messages::ShowMessage4(wxCommandEvent &event)
{
    wxMessageDialog *dial = new wxMessageDialog(NULL,
                                                wxT("Unallowed operation"), wxT("Exclamation"),
                                                wxOK | wxICON_EXCLAMATION);
    dial->ShowModal();
}

void Messages::ShowFileDialog(wxCommandEvent &event)
{
    wxFileDialog *dial = new wxFileDialog(this, wxT("Open file"), wxT(""), wxT(""),
                                          wxT("Text files (*.txt)|*.txt|All files (*.*)|*.*"),
                                          wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    if (dial->ShowModal() == wxID_OK)
    {
        wxMessageBox(wxT("You chose: ") + dial->GetPath());
    }
    dial->Destroy();
}

void Messages::ShowFontDialog(wxCommandEvent &event)
{
    wxFontDialog *dial = new wxFontDialog(this, wxFontData());
    if (dial->ShowModal() == wxID_OK)
    {
        wxFontData data = dial->GetFontData();
        wxFont font = data.GetChosenFont();
        wxColor col = data.GetColour();
        st->SetFont(font);
        st->SetForegroundColour(col);
        // wxMessageBox(wxT("You chose: ") + font.GetNativeFontInfoDesc());
        // wxMessageBox(wxT("You chose: ") + font.GetNativeFontInfoUserDesc());
    }
    dial->Destroy();
}

void Messages::ShowCustomDialog(wxCommandEvent &event)
{
    CustomDialog *dial = new CustomDialog( wxT("Custom Dialog"));
    dial->ShowModal();
}

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

bool MyApp::OnInit()
{
    Messages *frame = new Messages(wxT("Messages"));
    frame->Show(true);
    return true;
}

wxIMPLEMENT_APP(MyApp);